package cn.edu.hubu.hubuhotel.service.impl;

import cn.edu.hubu.hubuhotel.entity.Room;
import cn.edu.hubu.hubuhotel.mapper.RoomMapper;
import cn.edu.hubu.hubuhotel.service.RoomService;
import cn.edu.hubu.hubuhotel.vo.ResultVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.dao.DuplicateKeyException;

import java.util.List;

@Service
public class RoomServiceImpl implements RoomService {
    @Autowired
    private RoomMapper roomMapper;
    @Override
    public ResultVO checkRoombyroomId(int roomId){
        Room room=roomMapper.selectbyroomId(roomId);
        if(room!=null){
            return new ResultVO(200,"RoomId存在(不可用于添加操作)",room);
        }else{
            return new ResultVO(500,"RoomId不存在(可以用于添加操作)",null);
        }
    }
    @Override
    public ResultVO checkRoombyroomPrice(int price){
        List<Room> room =roomMapper.selectroombyprice(price);
        if(room.isEmpty()){
            return new ResultVO(500,"不存在该价位房间",null);
        }else{
            return new ResultVO(200,"查询成功",room);
        }
    }
    @Override
    public ResultVO checkRoombyroomTypeId(int roomTypeId){
        List<Room> room =roomMapper.selectroombyTypeId(roomTypeId);
        if(room.isEmpty()){
            return new ResultVO(500,"查询失败，没有该类型的房间",null);
        }else{
            return new ResultVO(200,"已选中该类型房间",room);
        }
    }
    @Override
    public ResultVO checkRoomall(int roomId,int price,int roomTypeId){
        Room room=roomMapper.selectRoomall(roomId,price,roomTypeId);
        if(room!=null){
            return new ResultVO(200,"查询成功",room);
        }else{
            return new ResultVO(500,"输入错误，查询失败",null);
        }
    }
    @Override
    public ResultVO addRoom(Room room){
        try {
            int i = roomMapper.insertRoom(room);
            if(i > 0){
                return new ResultVO(200, "插入新数据成功", null);
            } else {
                return new ResultVO(500, "插入新数据失败，请重试", null);
            }
        } catch (DuplicateKeyException e) {
            // 捕获主键冲突异常
            return new ResultVO(400, "插入数据失败：RoomId已经存在", null);
        } catch (Exception e) {
            // 捕获其他可能的异常
            return new ResultVO(500, "插入数据失败，请重试", null);
        }
    }
    @Override
    public ResultVO updateRoom(Room room){
        int i=roomMapper.updateRoom(room);
        if(i>0){
            return new ResultVO(200,"更新数据成功",null);
        }else {
            return new ResultVO(500,"更新数据失败，请重试",null);
        }
    }
    @Override
    public ResultVO deleteRoom(int roomId){
        int i=roomMapper.deleteRoom(roomId);
        if(i>0){
            return new ResultVO(200,"删除数据成功",null);
        }else {
            return new ResultVO(500,"删除数据失败，请重试",null);
        }
    }
    @Override
    public ResultVO checkRoomState(int roomId,int state){
        int i=roomMapper.updateRoomState(roomId,state);
        if(i>0){
            return new ResultVO(200,"更新数据成功",null);
        }else{
            return new ResultVO(500,"更新数据失败",null);
        }
    }
    @Override
    public ResultVO searchRoom(){
        List<Room> room =roomMapper.searchroom();
        if(room.isEmpty()){
            return new ResultVO(500,"获取房间信息失败",null);
        }else{
            return new ResultVO(200,"获取房间信息成功",room);
        }
    }
}
